package com.hsnn.medstgmini.sys.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.model.Whether;
import com.hsnn.medstgmini.sys.dao.UserInforDao;
import com.hsnn.medstgmini.sys.model.SysSequence;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.sys.service.UserInforManager;

@Repository
public class UserInforDaoImpl implements UserInforDao {

	@Autowired
	private UserInforManager userInforManager;

	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;

	/**
	 * @category 获取sequence
	 */
	public Long getSequence() {
		Long sequence = 10000000L;
		synchronized (SysSequence.class) {
			Session session = sessionFactory.getCurrentSession();
			Criteria criteria = session.createCriteria(SysSequence.class);
			criteria.add(Restrictions.eq("id", 1));
			SysSequence ss = (SysSequence) criteria.uniqueResult();
			if (ss == null) {
				ss = new SysSequence();
				ss.setId("1");
				ss.setSequence(sequence.toString());
				session.saveOrUpdate(ss);
				session.flush();
			} else {
				String seq = ss.getSequence();
				if (StringUtils.isNotBlank(seq)) {
					sequence = Long.parseLong(seq);
					sequence++;
					ss.setSequence(sequence.toString());
					session.saveOrUpdate(ss);
					session.flush();
				}
			}
		}
		return sequence;
	}

	/**
	 * @category 根据id查询用户
	 * @date 2015年6月17日
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public SysUser getUserById(String id) {
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(SysUser.class);
		criteria.add(Restrictions.eq("id", id));
		List<SysUser> list = criteria.list();
		if (list != null && !list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * @category 中心保存主用户信息
	 * @date 2015年6月14日
	 * @param user
	 */
	// public void zxUpdateUserInfor(int opType, SysUser user,
	// List<SysPrivilege> list) {
	// Session session = sessionFactory.getCurrentSession();
	// if (user != null && user.getRole() != null) {
	// // 保存角色信息
	// if (opType == Constants.OP_TYPE_EDIT) {
	// SysUser sysUser = getUserById(user.getId());
	// sysUser.setUpdUser(user.getUpdUser());
	// sysUser.setStatus(user.getStatus());
	// sysUser.setRemark(user.getRemark());
	// sysUser.setLocked(user.getLocked());
	// sysUser.setPhone(user.getPhone());
	// sysUser.setEmail(user.getEmail());
	// session.update(sysUser);
	// } else if (opType == Constants.OP_TYPE_ADD) {
	// // SysUser oldUser = getUserById(user.getId());
	// user.setCreateDateTime(new Date());
	// user.setAcctEnabled(Whether.yes.getKey());
	// session.update(user);
	// }
	//
	// session.flush();
	// }
	// }

	@Override
	public void zxUpdateUserInfor(SysUser sysUser) {
		Session session = sessionFactory.getCurrentSession();
		if (sysUser != null && sysUser.getId() != null) {
			SysUser user = getUserById(sysUser.getId());
			if (user != null) {
				user.setPhone(sysUser.getPhone());
				user.setRemark(sysUser.getRemark());
				if (user.getMain() == Whether.no.getKey()) {
					user.setName(sysUser.getName());
				}
				
				session.saveOrUpdate(user);
				session.flush();
			}
		}
		session.flush();
	}

}
